<template>
  <view class="search-history" v-if="history.length > 0">
    <view class="header">
      <text class="title">搜索历史</text>
      <uni-icons type="trash" size="20" color="#999" @click="clearHistory"></uni-icons>
    </view>
    <view class="history-list">
      <view 
        class="history-item"
        v-for="(item, index) in history"
        :key="index"
        @tap="onSelect(item)"
      >
        <uni-icons type="search" size="14" color="#999"></uni-icons>
        <text class="keyword">{{ item }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    history: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    onSelect(keyword) {
      this.$emit('select', keyword)
    },
    clearHistory() {
      uni.showModal({
        title: '提示',
        content: '确定要清空搜索历史吗？',
        success: (res) => {
          if (res.confirm) {
            this.$emit('clear')
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-history {
  padding: 20rpx;
  background-color: #fff;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;

    .title {
      font-size: 28rpx;
      color: #333;
      font-weight: bold;
    }
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;

    .history-item {
      display: flex;
      align-items: center;
      padding: 12rpx 24rpx;
      background-color: #f5f5f5;
      border-radius: 32rpx;

      .uni-icons {
        margin-right: 8rpx;
      }

      .keyword {
        font-size: 24rpx;
        color: #666;
      }
    }
  }
}
</style> 